<script>
// 新增编辑页，弹窗会另抽出组件
import { defineComponent } from 'vue'
export default defineComponent({
  components: { EditQrBase, EditQrWelcome, EditQrFunc },
	name: 'EditQr'
})
</script>
<script setup>
import { deepClone } from '@/utils'
import { qrFormData,qrGroupListData,qrOrignalForm } from '@/data'
import {reqQrGroupList,reqQrUpdate,reqQrGetone} from '@/api'
import { toRaw } from '@vue/reactivity';
import { ref,defineExpose,reactive } from 'vue'
import MyIcon from '@/components/my-icon/index.vue'
import EditQrBase from '../list/editQr/base.vue'
import EditQrWelcome from '../list/editQr/welcome.vue'
import EditQrFunc from '../list/editQr/function.vue';
const editQrBase = ref()
const editQrWelcome = ref()
const editQrFunc = ref()
const groupList = ref()

const isRefresh = ref(true)
const isNew = ref(true)
const dialogVisible = ref(false)
const form = ref({})
const show =async (val)=>{
	console.log(val,'sdsdsd')
	
	if (val){
		isNew.value = false
		const r1 = await reqQrGetone(val)
		if (r1) {
			var temp = r1.data
			var beZero = ['customerdes_status','customerbz_status','staff_status','tag_status','groupid','skipverify_type','staffline_status','customerbzqh_status','welcome_type','dayparting_status']
			beZero.forEach(item=>{
				if (!temp[item])  temp[item]=0
			})
			var beArray = ['staffid','staff_standby','tag','welcomeOtherData']
			beArray.forEach(item=>{
				if (temp[beArray]==null)  temp[item]=[]
			})
			if (temp['staff_type']==null){
				temp['staff_type'] = 1
			}
			if (temp['staff_data']==null){
				temp['staff_data'] = [
					{
						selectStaffData: [],
						selectstaffids: [],
						time: [],
						week: []
					}
	]
			}else{
				if (temp['staff_type']==2) {
					
				}
				
			}
			

			form.value = Object.assign(form.value,temp)
			console.log(form.value);
		}
		console.log(toRaw(form.value),'根据拿到的id获取form数据')
	}else{
		console.log('asdasdasd');
		form.value = deepClone(qrFormData);
	}
	
  const r = await reqQrGroupList()
	groupList._value = r.data
	dialogVisible.value = true;
  // handleRefresh()
  console.log(form.value,groupList.value,'lllllllllllllll');
}

const validWeekTime=(json,staffDataTime,staff_data,standbyData)=>{
	console.log(staffDataTime.length,staff_data.length,'ooooooooooo')
	if(json.staff_type==2&&staffDataTime.length!=staff_data.length){
			ElMessage({
				message: '未添加使用成员',
				type: 'error',
			})
		return false
	}else if(json.staff_type==2&&standbyData.length==0){
			ElMessage({
				message: '自动上下线，未添加备用成员',
				type: 'error',
			})
		return false
	}
	return true
}
const submit = async(formEl) => {
	console.log(toRaw(form.value),'form表单')
	var json = toRaw(form.value)
	var staffDataTimeTemp = toRaw(editQrBase.value.getExposeData().staffTimeArr)
	var staffDataTime = []
	console.log(staffDataTime,'huouiouio')
	for(var reTemp in staffDataTimeTemp){
			console.log(reTemp,'890890890')
		staffDataTime[reTemp] = toRaw(staffDataTimeTemp[reTemp].value)
	}
	console.log(staffDataTime)
	var staff_data = toRaw(editQrBase.value.getExposeData().staff_data)
	var staffupData = toRaw(editQrBase.value.getExposeData().staffupData)
	var standbyData = toRaw(editQrBase.value.getExposeData().standbyData)
	var skipverify_time = toRaw(editQrFunc.value.getExposeData().skipverify_time)
	console.log(standbyData)
	if(json.staff_type!=2){
		staff_data = ['']
	}
	var staffData = []
	
	console.log(staff_data,'llllllllllll');
	if (validWeekTime(json,staffDataTime,staff_data,standbyData)){
		console.log(standbyData)
			if(skipverify_time){
				json.skipverify_time = skipverify_time
			}
			staff_data.forEach((item1,index)=>{
				
			var staffJson = {
				staffid:[],
				week:'',
				startime:json.staff_type==2?staffDataTime[index].value[0]:'',
				endtime:json.staff_type==2?staffDataTime[index].value[1]:'',
			}
			console.log(staffJson,'uioipoip')
				console.log(json.staff_type,item1.selectStaffData,'56456ggg90890890890')
		// item.week = item.week.join(',')
				if(json.staff_type==2){
					console.log('sdfsdfsdfsdf')
					item1.selectStaffData.forEach(item=>{

						staffJson.staffid.push(item.id)
					})
				}else{
					console.log(json.staffid,'klklkkll')
					json.staffid.forEach(item=>{
						staffJson.staffid.push(item.id)
					})
				}
				
				try{
					staffJson.week = item1.week.join(',')
				}catch{
					console.log(staffJson.week);
				}
				staffData.push(staffJson)

				console.log(item1)
				// item.time = item.week.join('~')
			})
	console.log(staffData)
	json.staff_data = staffData
	
	console.log(staffupData)
	if(staffupData){
			staffupData.forEach(item=>{
			item.staffid = item.id
		})
	}

	json.staffup_data = staffupData
	if(json.staffup_status!=1){
		delete json.staffup_data
	}else{
		// var temp = json.staffup_data
		// var ids = []
		// temp.forEach(item=>{
		// 	ids.push(item.id)
		// })
		// json.staffup_data = ids
	}
	// if(json.staff_type!=2){
		// var staff_standbyid = []
		// console.log(json.staff_standby)
		// json.staff_standby.forEach((item,index)=>{
		// 	staff_standbyid.push(item.id)
		// })
		
		// json.staff_standby = staff_standbyid
		console.log(json.staff_standby)
		var staffIds = []
		console.log(json.staffid)
		json.staffid.forEach(item=>{
			staffIds.push(item.id)
		})
		json.staffid=staffIds
		if(json.staff_type!=2){
			console.log('aaaaaaaaaaaaa')
			json.staff_data = []
			json.staff_standby = standbyData || []
			

			console.log(standbyData,'jhkhjkhjk');
		}else{
			delete json.staffupData
			delete json.staffidTime
			var tempStaffBy = standbyData
			var arr = []
			tempStaffBy.forEach(item=>{
				arr.push(item.id)
			})
			console.log(standbyData,'11111aaaaaaaaaaaaa')
			json.staff_standby = arr
			
		}
		delete json.create_time
		delete json.update_time

	// }else{
	// 	json.staffid = []
	// }
	

	delete json.create_time


	// json.week = week.value.join(',')

	// json.time = time.value.join('~')
	console.log(json,'保存二维码编辑接口')
	json.staff_standby=standbyData
	const r =  await reqQrUpdate(json)
	if(r){
		dialogVisible.value=false
		emits('goBack')
	}
	}
	
	



	
}
const cancel = ()=>{
	form._value = qrOrignalForm
	dialogVisible.value=false
	emits('goBack')
}
const change = (e) => {};
const emits = defineEmits(['goBack'])
defineExpose({
  show
});
</script>
<template>
	<div v-if="dialogVisible&&isRefresh">
		<div class="app-back-tab">
			<div class="app-flex-left">
				<span class="back-box app-pointer" @click="cancel">
					<MyIcon class="app-icon " name="Back" />
					<span class="back-box-mr">返回</span>
				</span>
				<span class="back-box-gap">|</span>
				<span class="back-title">创建活码</span>
			</div>
		</div>
		<EditQrBase ref="editQrBase" :editForm="form" :groupListData="groupList" />
		<EditQrWelcome ref="editQrWelcome" :editForm="form" />
		<EditQrFunc ref="editQrFunc" :editForm="form" />
			<div class="app-edit app-gap-block app-container app-flex-center">
			<el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit"
          >保存</el-button
        >
			</div>
	</div>
</template>
<style lang="scss">
</style>